package cn.fl.insurance.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.fl.preloan.IInsPlcBscInfoService;
import cn.fl.preloan.condition.PrjPrdDtlForInsQueryCondition;
import cn.fl.preloan.dto.insurance.GetInsureListRequest;
import cn.fl.preloan.dto.insurance.PageResponse;
import cn.fl.commons.result.ResponseData;
import cn.fl.preloan.dto.insurance.PrjPrdDtlForInsDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;

import cmm.mid.core.framework.page.ListPageVO;
import cmm.mid.core.framework.page.Pager;
import cmm.web.core.framework.controller.BaseController;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.project.transform.PrjPrdDtlForInsVOTransform;
import cn.fl.project.vo.PrjPrdDtlForInsVO;

/**
 * Function:投保管理
 * Created by Administrator on 2016/8/17.
 */
@Controller
@RequestMapping("/insurance/insInsure")
public class InsInsureController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(InsInsureController.class);

    /**
     * 贷前服务
     */
    @Autowired
    private IInsPlcBscInfoService iInsPlcBscInfoService;

    /**
     * 到投保管理列表页面
     */
    @RequestMapping(value = "/toListPage.do", method = {RequestMethod.GET})
    public ModelAndView toInsureListPage(HttpServletRequest request, HttpServletResponse response) {
        BaseLogUtils.info(logger, "toInsureListPage", "加载投保列表页面.", CurrentThreadContext.getCurrentUserName());
        ModelAndView mav = new ModelAndView("insurance/insPlcBscInfo/insInsureList");
        return mav;
    }


    /**
     * 初始化列表,投保管理列表
     */
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "/initList.do", method = {RequestMethod.GET})
    @ResponseBody
    public Object initInsureList(HttpServletRequest request, HttpServletResponse response) {
        String currentPage = request.getParameter("page");
        String pageSize = request.getParameter("pagesize");
        Pager page = new Pager();
        if (StringUtils.isNotEmpty(currentPage)) {
            page.setCurrentPage(Integer.parseInt(currentPage));
        }
        if (StringUtils.isNotEmpty(pageSize)) {
            page.setEveryPage(Integer.parseInt(pageSize));
        }

        PrjPrdDtlForInsQueryCondition condition = new PrjPrdDtlForInsQueryCondition();
        //设置查询条件？
        String cstNm = request.getParameter("cstNm");//客户名称
        if (StringUtils.isNotEmpty(cstNm)) {
            condition.setCstNm(cstNm);
        }
        String paySchNo = request.getParameter("paySchNo");//支付表编号
        if(StringUtils.isNotBlank(paySchNo)){
            condition.setPaySchNo(paySchNo);
        }
        String lsCntNo=request.getParameter("lsCntNo");//融资租赁合同号
        if(StringUtils.isNotBlank(lsCntNo)){
            condition.setLsCntNo(lsCntNo);
        }
        condition.setUsrId(CurrentThreadContext.getCurrentUserId());

        //调用贷前服务
        ResponseData responseData = iInsPlcBscInfoService.searchInsureListPageByCondition(page.getEveryPage(), page.getCurrentPage(), condition,
                CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd());
        BaseLogUtils.info(logger, "initList", "贷前服务--初始化投保管理列表页面数据.request:" + JSONObject.toJSONString(condition)
                + ",response: " + JSONObject.toJSONString(responseData), CurrentThreadContext.getCurrentUserName());

        PageResponse<PrjPrdDtlForInsDTO> pageResponse = null;
        List<PrjPrdDtlForInsVO> list = new ArrayList<>();
        PrjPrdDtlForInsVO prjPrdDtlForInsVO = null;
        if (responseData != null && responseData.getData() != null) {
            pageResponse = (PageResponse<PrjPrdDtlForInsDTO>) responseData.getData();
            List<PrjPrdDtlForInsDTO> pageResponseListlist = pageResponse.getList();
            for (PrjPrdDtlForInsDTO prjPrdDtlForInsDTO : pageResponseListlist) {
                prjPrdDtlForInsVO = new PrjPrdDtlForInsVO();
                prjPrdDtlForInsVO.setId(prjPrdDtlForInsDTO.getId());
                BeanUtils.copyProperties(prjPrdDtlForInsDTO,prjPrdDtlForInsVO);
                list.add(prjPrdDtlForInsVO);
            }
        }
        /*Pager pager = this.insPlcBscInfoService.searchInsureListPage(page);
        List<PrjPrdDtlForInsVO> list = (List<PrjPrdDtlForInsVO>) pager.getResultList();*/
        ListPageVO<PrjPrdDtlForInsVO> listPage = new ListPageVO<PrjPrdDtlForInsVO>(list, pageResponse.getTotalCount());
        BaseLogUtils.info(logger, "initList", "贷前服务--投保管理列表.response:" + JSONObject.toJSONString(listPage), CurrentThreadContext.getCurrentUserName());

        return listPage;
    }

    /**
     * 根据项目ID初始化投保管理列表
     */
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "/searchList.do", method = {RequestMethod.GET})
    @ResponseBody
    public Object searchList(HttpServletRequest request, HttpServletResponse response) {
        PrjPrdDtlForInsQueryCondition qc=new PrjPrdDtlForInsQueryCondition();
        //设置查询条件？
        String prjId = request.getParameter("prjId");//项目ID
        String payStsCd = request.getParameter("payStsCd");//付款状态
        String insStsCd = request.getParameter("insStsCd");//投保状态
        String paySchId = request.getParameter("paySchId");//支付表ID
        if (StringUtils.isNotEmpty(prjId)) {
            qc.setPrjId(Long.valueOf(prjId));
        }else{
            return new ListPageVO<PrjPrdDtlForInsVO>(null, 0);
        }
        if(StringUtils.isNotBlank(payStsCd)){
            qc.setPayStsCd(payStsCd);
        }
        if(StringUtils.isNotBlank(insStsCd)){
            qc.setInsStsCd(insStsCd);
        }
        if(StringUtils.isNotEmpty(paySchId)){
            qc.setPaySchId(Long.valueOf(paySchId));
        }
        qc.setSysCd(CurrentThreadContext.getCurrentSysCd());
        qc.setOrgCd(CurrentThreadContext.getCurrentOrgCd());
        qc.setUsrId(CurrentThreadContext.getCurrentUserId());

        BaseLogUtils.info(logger, "initList", "初始化投保管理列表页面数据(流程中呈报人维护保单):", CurrentThreadContext.getCurrentUserName());

        GetInsureListRequest getRequest = new GetInsureListRequest(CurrentThreadContext.getCurrentSysCd(),CurrentThreadContext.getCurrentOrgCd(),qc);
        ResponseData<List<PrjPrdDtlForInsDTO>> responseData = this.iInsPlcBscInfoService.searchInsureList(getRequest);
        List<PrjPrdDtlForInsVO> list = null;
        if (null != responseData && ResponseData.SUCCESS_CODE.equals(responseData.getCode()) && CollectionUtils.isNotEmpty(responseData.getData())){
            List<PrjPrdDtlForInsDTO> data = responseData.getData();
            list = new ArrayList<>(data.size());
            PrjPrdDtlForInsVO vo = null;
            for (PrjPrdDtlForInsDTO dto : data) {
                vo = new PrjPrdDtlForInsVO();
                BeanUtils.copyProperties(dto,vo);
                list.add(vo);
            }
        }

        ListPageVO<PrjPrdDtlForInsVO> listPage = null;
        if(CollectionUtils.isNotEmpty(list)){
            listPage = new ListPageVO<PrjPrdDtlForInsVO>(list, list.size());
        }else{
            listPage = new ListPageVO<PrjPrdDtlForInsVO>(null, 0);
        }
        return listPage;
    }

    /**
     * 初始化列表，不用
     * @Deprecated
     */
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "/initPrdList.do", method = {RequestMethod.GET})
    @ResponseBody
    public Object initInsurePrdList(HttpServletRequest request, HttpServletResponse response) {
        //设置查询条件
        String paySchId = request.getParameter("paySchId");//项目产品明细Id
        PrjPrdDtlForInsQueryCondition qc=new PrjPrdDtlForInsQueryCondition();
        if(StringUtils.isEmpty(paySchId)){
            return null;
        }
        qc.setPaySchId(Long.valueOf(paySchId));//支付表Id
        //qc.setSysCd(CurrentThreadContext.getCurrentSysCd());
        //qc.setOrgCd(CurrentThreadContext.getCurrentOrgCd());
        BaseLogUtils.info(logger, "initInsurePrdList", "初始化投保管理产品明细列表数据.DATA:{paySchId：" + paySchId + "}", CurrentThreadContext.getCurrentUserName());
        GetInsureListRequest getRequest = new GetInsureListRequest(CurrentThreadContext.getCurrentSysCd(),CurrentThreadContext.getCurrentOrgCd(),qc);
        ResponseData<List<PrjPrdDtlForInsDTO>> responseData = this.iInsPlcBscInfoService.searchInsureList(getRequest);
        List<PrjPrdDtlForInsVO> list = null;
        if (null != responseData && ResponseData.SUCCESS_CODE.equals(responseData.getCode()) && CollectionUtils.isNotEmpty(responseData.getData())){
            List<PrjPrdDtlForInsDTO> data = responseData.getData();
            list = new ArrayList<>(data.size());
            PrjPrdDtlForInsVO vo = null;
            for (PrjPrdDtlForInsDTO dto : data) {
                vo = new PrjPrdDtlForInsVO();
                BeanUtils.copyProperties(dto,vo);
                list.add(vo);
            }
        }

        if(list==null||list.isEmpty()){
            return null;
        }
        ListPageVO<PrjPrdDtlForInsVO> listPage = new ListPageVO<PrjPrdDtlForInsVO>(list, list.size());
        return listPage;
    }

}
